package cn.sh.library.gmwx.sparql.hsly.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import cn.sh.library.gmwx.annotation.GraphDefine;
import cn.sh.library.gmwx.base.Constant;
import cn.sh.library.gmwx.bean.ArchitectureSearchBean;
import cn.sh.library.gmwx.dao.impl.BaseDaoImpl;
import cn.sh.library.gmwx.dto.QueryResult;
import cn.sh.library.gmwx.sparql.RdfQuery;
import cn.sh.library.gmwx.sparql.SparqlExecution;
import cn.sh.library.gmwx.sparql.hsly.ArchitectureSparql;
import cn.sh.library.gmwx.utils.DateUtilC;
import cn.sh.library.gmwx.utils.RDFUtils;


@Repository
@GraphDefine(name = Constant.GRAPH_BUILDING)
public class ArchitectureSparqlImpl extends BaseDaoImpl implements ArchitectureSparql {

	@Override
	public QueryResult<Map<String, Object>> getArchitectureList(ArchitectureSearchBean search, int start, int size) {


		QueryResult<Map<String, Object>> result = new QueryResult<>();
		ArrayList results = new ArrayList();

		// 拼接sparql
		String filter1 = "";
		String filter2 = "";
		
		if (StringUtils.isNotBlank(search.getFreetext())) {
			
			filter2 += ".filter (contains(str(?o),'"
					+ search.getFreetext() + "'))";

		}
//		if(StringUtils.isNotBlank(search.getIsRed())) {
//			filter1 += ".{?uri dc:type ?type}.filter (?type = '红色旅游景点')";
//		}
		if("1".equals(search.getIsRed())) {
			filter1 += ".{?uri dc:type ?type}.filter (?type ='红色旅游景点')";
			
		}
		if("2".equals(search.getIsRed())) {
			filter1 += ".{?uri dc:type ?type}.filter (?type ='上海市不可移动文物名录')";
		}
		if("3".equals(search.getIsRed())) {
			filter1 += ".{?uri dc:type ?type}.filter (?type ='上海市优秀历史建筑')";
		}
		
		
		// 查询uri数量
		results = SparqlExecution
				.vQuery(graph,
						RdfQuery.RDF_QUERY_ARCHITECTURE_COUNT.toString(filter1,filter2),
						"count");
		Long count = Long.parseLong(RDFUtils.getValue(((Map) results.get(0))
				.get("count").toString()));
		result.setTotalrecord(count);
		
		// 查询Uri列表封装入List
		
		results = SparqlExecution.vQuery(graph,
				RdfQuery.RDF_QUERY_ARCHITECTURE_IRI.toString(filter1,filter2, start,
						size), "uri");
		
		Date d1 = DateUtilC.getNowDateTime();
		System.out.println("----执行开始：" + DateUtilC.getNowDateTime());

		String uris = "";
		List _list = null;
		
		//遍历装uri的List装入数组
		for (int i = 0; i < results.size(); i++) {
			String uri = ((Map) results.get(i)).get("uri").toString();
			uris += "<" + uri + ">";
		}
		
		//把数组放入sparql查询
		_list = RDFUtils.transformListMap2(SparqlExecution.vQuery(graph,
				RdfQuery.RDF_QUERY_ARCHITECTURE_SIMPLE.toString(uris),
				new String[] { "uri", "nameS", "nameT", "nameE", "address",
						"type", "des", "houseNumber", "placeUri" }));
		
		Date d2 = DateUtilC.getNowDateTime();
		long interval = (d2.getTime() - d1.getTime()) / 1000;
		System.out.println("执行用时：" + interval + "秒");// 
		result.setResultList(_list);

		return result;
	}
	
	

	@Override
	public Map<String, Object> getArchitectureDetail(String uri) {

		Map<String, Object> info_map = new HashMap<String, Object>();
		List _architectureList = null;
		List _eventList = null;
		
		_architectureList = RDFUtils.transformListMap2(SparqlExecution.vQuery(
				graph, RdfQuery.RDF_QUERY_ARCHITECTURE_DETAIL.toString(uri), new String[] { "uri", "address", "nameE",
						"nameS", "nameT", "source", "type",
						"placeUri", "lat", "long", "des","houseNumber" }));
		
		Map<String, Object> _objMap = (Map) _architectureList.get(0);
		
		_eventList = RDFUtils.transformListMap2(SparqlExecution.vQuery(graph,
				RdfQuery.RDF_QUERY_EVENT_SIMPLE.toString(uri), new String[] {
						"description","startedAtTime", "endedAtTime" }));
		
		_objMap.put("eventList", _eventList);
		info_map.put("data", _objMap);
		return info_map;
	}



	@Override
	public List<Map<String, String>> getRDF(String g, String uri) {
		
		if(!uri.startsWith("http")){
			uri="nodeID://"+uri;
		}
		
		return RDFUtils.transformListMap(SparqlExecution.vQuery(graph, RdfQuery.RDF_QUERY.toString(g,uri),
				new String[] { "s", "p", "o" }));
	}

}
